package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserRegisterResponse;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 17:17 2021/4/21
 */
@Slf4j
@Service
@org.springframework.stereotype.Service
public class VerifyServiceImpl implements IVerifyService {
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;
    @Override
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse response = new UserVerifyResponse();
        //判空验证
        userVerifyRequest.requestCheck();
        //更新用户验证表激活状态
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid", userVerifyRequest.getUuid());
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        int effectRow = userVerifyMapper.updateByExampleSelective(userVerify, example);
        if(effectRow != 1){
            response.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            response.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return response;
        }
        //更新用户表激活状态
        Example example1 = new Example(Member.class);
        example1.createCriteria().andEqualTo("username", userVerifyRequest.getUserName());
        Member member = new Member();
        member.setIsVerified("Y");
        int effectRow1 = memberMapper.updateByExampleSelective(member, example1);
        if(effectRow != 1){
            response.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            response.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return response;
        }
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }
}
